package cn.com.dcsgo.controller;


import cn.com.dcsgo.model.activity.ActivityInfo;
import cn.com.dcsgo.model.product.SkuInfo;
import cn.com.dcsgo.result.Result;
import cn.com.dcsgo.service.ActivityInfoService;
import cn.com.dcsgo.vo.activity.ActivityRuleVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/*
请求路径及其参数参考前端：
uu-preferred-front-end/uuyx-admin/src/api/activity/activityInfo.js
 */

/**
 * <p>
 * 活动表 前端控制器
 * </p>
 *
 * @author Dcsgo
 * @since 2025-04-19
 */
@RequiredArgsConstructor
@Api(tags = "营销活动管理")
@RestController
@RequestMapping("admin/activity/activityInfo")
public class ActivityInfoController {
    private final ActivityInfoService activityInfoService;

    @ApiOperation("分页获取活动列表")
    @GetMapping("{page}/{size}")
    public Result<IPage<ActivityInfo>> getPageList(@PathVariable Long page,
                                                   @PathVariable Integer size) {
        Page<ActivityInfo> pageParam = new Page<>(page, size);
        IPage<ActivityInfo> result = activityInfoService.getPage(pageParam);
        return Result.ok(result);
    }

    @ApiOperation("通过id获取活动信息")
    @GetMapping("get/{id}")
    public Result<ActivityInfo> getById(@PathVariable Long id) {
        ActivityInfo activityInfo = activityInfoService.getById(id);
        activityInfo.setActivityTypeString(activityInfo.getActivityType().getComment());
        return Result.ok(activityInfo);
    }

    @ApiOperation("新增活动")
    @PostMapping("save")
    public Result<Serializable> save(@RequestBody ActivityInfo activityInfo) {
        activityInfoService.save(activityInfo);
        return Result.ok(null);
    }

    @ApiOperation("通过id修改活动")
    @PutMapping("update")
    public Result<Serializable> updateById(@RequestBody ActivityInfo activityInfo) {
        activityInfoService.updateById(activityInfo);
        return Result.ok(null);
    }

    @ApiOperation("通过id删除活动")
    @DeleteMapping("remove/{id}")
    public Result<Serializable> removeById(@PathVariable Long id) {
        activityInfoService.removeById(id);
        return Result.ok(null);
    }

    @ApiOperation("通过id批量删除活动")
    @DeleteMapping("batchRemove")
    public Result<Serializable> batchRemove(@RequestBody List<Long> idList) {
        activityInfoService.removeByIds(idList);
        return Result.ok(null);
    }

    @ApiOperation("通过id获取活动规则信息")
    @GetMapping("findActivityRuleList/{id}")
    public Result<Map<String, Object>> findActivityRuleList(@PathVariable Long id) {
        Map<String, Object> result = activityInfoService.findActivityRuleList(id);
        return Result.ok(result);
    }

    @ApiOperation("添加活动规则")
    @PostMapping("saveActivityRule")
    public Result<Serializable> saveActivityRule(@RequestBody ActivityRuleVo activityRuleVo) {
        activityInfoService.saveActivityRule(activityRuleVo);
        return Result.ok(null);
    }

    @ApiOperation(value = "根据sku name 获取sku列表", notes = "排除不可添加活动的sku")
    @GetMapping("findSkuInfoByKeyword/{keyword}")
    public Result<List<SkuInfo>> findSkuInfoByKeyword(@PathVariable String keyword) {
        List<SkuInfo> skuInfoList = activityInfoService.findSkuInfoBySkuName(keyword);
        return Result.ok(skuInfoList);
    }
}

